# Table 1: Correlations between high/low income/education revealed preferences
fig4_dat = bind_rows(import(here("data","fig4_inc_agree_dat.rds")) |> 
                       mutate(agree = "Agree"),
                     import(here("data","fig4_inc_disagree_dat.rds")) |> 
                       mutate(agree = "Disagree")) |> 
  invert_group()

fig6_dat = bind_rows(import(here("data","fig6_edu_agree_dat.rds")) |> 
                       mutate(agree = "Agree"),
                     import(here("data","fig6_edu_disagree_dat.rds")) |> 
                       mutate(agree = "Disagree")) |>
  invert_group()

fig8_dat = bind_rows(import(here("data","fig8_r_edu_agree_dat.rds")) |> 
                       mutate(agree = "Agree"),
                     import(here("data","fig8_r_edu_disagree_dat.rds")) |> 
                       mutate(agree = "Disagree")) |> 
  invert_group()

## Correlations by Redistribution ##

cor_mod = \(data, agree_in, redist_in){
  econ = c("gov_insurance", "public_option", "health_subsidies",
           "mctaxes", "estate_tax", "uctaxes", "college",
           "trade", "minwage", "gauranteed_jobs", "right_to_work",
           "medicare_for_all", "uctaxes2", "abolish_priv_insurance",
           "china_tariffs", "forgive_debt", "wealth_tax","housing_guarantee")
  dat = left_join(data %>% filter(group==1, agree == agree_in),
                  data %>% filter(group==0, agree == agree_in),
                  by="condition") |> 
    mutate(is_redist = as.character(condition %in% econ))
  if(redist_in != "Pooled"){
    dat = dat |> filter(is_redist == redist_in)
  }
  data.frame(
    agree = agree_in,
    redist = redist_in,
    cor = with(dat, cor(effect.x,effect.y))
  )
}

run_cor = \(data, demo){
  # Run models
  vars = expand.grid(c("Pooled", "TRUE","FALSE"),
                     c("Agree","Disagree"))
  map2_dfr(vars$Var2,vars$Var1, ~ cor_mod(data = data, agree_in = .x, redist_in = .y)) |> 
    mutate(demo = demo) |> 
    remove_rownames()
}

cor_df = bind_rows(
  run_cor(fig4_dat, "Income"),
  run_cor(fig6_dat, "Education")
) |> 
  mutate(across(where(is.numeric), ~round(.x, 2)),
         redist = case_match(
           redist,
           "Pooled" ~ "Pooled",
           "TRUE" ~ "Redistributive",
           "FALSE" ~ "Non-Redistributive"
         ),
         demo_agree = paste0(demo, agree)) |> 
  pivot_wider(id_cols = redist, names_from = demo_agree, values_from = cor)

kable(cor_df,
      col.names = c("Policy Class","Agree","Disagree",
                    "Agree","Disagree"),
      format = 'latex', booktabs = T) |> 
  kable_classic(full_width = F) |> 
  add_header_above(c(" " = 1, "Income" = 2, "Education" = 2)) |> 
  fix_table() |> 
  writeLines(here("tables","tab_1.tex"))

ccat("See 'tab_1.tex' for Table 1 output")

#### Appendix Table S9 ####

cor_df_rep = bind_rows(
  run_cor(fig8_dat, "Education (Republican)")
) |> 
  mutate(across(where(is.numeric), ~round(.x, 2)),
         redist = case_match(
           redist,
           "Pooled" ~ "Pooled",
           "TRUE" ~ "Redistributive",
           "FALSE" ~ "Non-Redistributive"
         ),
         demo_agree = paste0(demo, agree)) |> 
  pivot_wider(id_cols = redist, names_from = demo_agree, values_from = cor)

kable(cor_df_rep,
      col.names = c("Policy Class","Agree","Disagree"),
      format = 'latex', booktabs = T) |> 
  kable_classic(full_width = F) |> 
  add_header_above(c(" " = 1,"Education (Rep.)" = 2)) |> 
  fix_table() |> 
  writeLines(here("tables","appendix","tab_s9.tex"))

ccat("See 'tab_s9.tex' for Table S9 output")
